package com.slj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slj.mapper.UserMapper;
import com.slj.model.entity.Squad;
import com.slj.mapper.SquadMapper;
import com.slj.model.entity.User;
import com.slj.model.vo.SquadVo;
import com.slj.service.SquadService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.slj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 小组表 服务实现类
 * </p>
 *
 * @author 礼俊
 * @since 2025-10-22 10:20
 */
@Service
public class SquadServiceImpl extends ServiceImpl<SquadMapper, Squad> implements SquadService {
    @Autowired
    private SquadMapper squadMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Squad> getAllSquads() {
        List<Squad> allSquads = this.list();
        return allSquads;
    }

    @Override
    public List<SquadVo> getSquadsAndUsers(String name) {
            LambdaQueryWrapper<Squad> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(Squad::getName, name);
            //查询所有小组
            List<Squad> allSquads =this.list( wrapper);
            List<SquadVo> squadVos = allSquads.stream().map(squad -> {
                SquadVo squadVo = new SquadVo();
                squadVo.setId(squad.getId());
                squadVo.setName(squad.getName());
                squadVo.setNum(squad.getNum());
                squadVo.setIntegral(squad.getIntegral());
                squadVo.setSlogan(squad.getSlogan());
                return squadVo;
            }).toList();
        squadVos.forEach(squad -> {
            LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(User::getSquadId, squad.getId()); // 使用实体类的get方法，更安全
            List<User>  users= userService.list(userWrapper);
            squad.setUsers(users);
        });
        return squadVos;
    }
    
//    public List<SquadVo> getSquadsAndUsersByName() {
//        LambdaQueryWrapper<Squad> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(Squad::getName, name);
//        List<Squad> allSquads = this.list( wrapper);
//        List<SquadVo> squadVos;
//        squadVos = allSquads.stream().map(squad -> {
//            SquadVo squadVo = new SquadVo();
//            squadVo.setId(squad.getId());
//            squadVo.setName(squad.getName());
//            squadVo.setNum(squad.getNum());
//            squadVo.setIntegral(squad.getIntegral());
//            squadVo.setSlogan(squad.getSlogan());
//            return squadVo;
//        }).toList();
//        squadVos.forEach(squad -> {
//            LambdaQueryWrapper<User> wrapperUser = new LambdaQueryWrapper<>();
//            wrapperUser.eq(User::getSquadId, squad.getId()); // 使用实体类的get方法，更安全
//            List<User>  users= userService.list(wrapperUser);
//            squad.setUsers(users);
//        });
//        return squadVos;
//    }
}
